package com.bona.dubbo.system.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.bona.dubbo.system.ISysDepartDubboSV;
import com.bona.model.system.SysOrgModel;
import com.bona.model.system.SysUserOrgModel;
import com.bona.service.system.SysOrgService;
import com.bona.service.system.SysUserService;

public class SysDepartDubboSVImpl implements ISysDepartDubboSV {
	
	@Autowired
	private SysOrgService orgService;
	@Autowired
	private SysUserService userService;

	@Override
	public SysOrgModel query(SysOrgModel model) throws Exception {
		
		return orgService.query(model);
	}

	@Override
	public Integer queryByCount(SysOrgModel model) {
		
		Integer count = orgService.queryByCount(model);
		return count;
	}

	@Override
	public List<SysOrgModel> queryByPage(SysOrgModel model) {
		
		List<SysOrgModel> orgs = orgService.queryByPage(model);
		return orgs;
	}

	@Override
	public Integer delete(SysOrgModel model) throws Exception {
		return orgService.delete(model);
	}

	@Override
	public void add(SysOrgModel model) {
		orgService.add(model);
	}
	
	@Override
	public String queryMaxOrgCode(String orgCode, int localCodeLen) {
		
		return orgService.queryMaxOrgCode(orgCode, localCodeLen);
	}

	@Override
	public void deleteUserOrg(SysUserOrgModel model) {
		orgService.deleteUserOrg(model);
	}

	@Override
	public void addUserToOrg(String departId, String userIds) {
		userService.addUserToOrg(departId, userIds);
	}

	@Override
	public List<SysOrgModel> queryByPageRecursive(SysOrgModel model) {
		
		List<SysOrgModel> departList = orgService.queryByPage(model);
		for (SysOrgModel org1: departList) {
			SysOrgModel qorg1 = new SysOrgModel();
			qorg1.setParentdepartid(org1.getId());
			List<SysOrgModel> childDepart1 = orgService.queryByPage(qorg1);
			for (SysOrgModel org2: childDepart1) {
				SysOrgModel qorg2 = new SysOrgModel();
				qorg2.setParentdepartid(org2.getId());
				List<SysOrgModel> childDepart2 = orgService.queryByPage(qorg2);
				for (SysOrgModel org3: childDepart2) {
					SysOrgModel qorg3 = new SysOrgModel();
					qorg3.setParentdepartid(org3.getId());
					List<SysOrgModel> childDepart3 = orgService.queryByPage(qorg3);
					for (SysOrgModel org4: childDepart3) {
						SysOrgModel qorg4 = new SysOrgModel();
						qorg4.setParentdepartid(org4.getId());
						List<SysOrgModel> childDepart4 = orgService.queryByPage(qorg4);
						org3.setChildDepart(childDepart4);
					}
					org3.setChildDepart(childDepart3);
				}
				org2.setChildDepart(childDepart2);
			}
			org1.setChildDepart(childDepart1);
		}
		
		return departList;
	}

}
